package com.example.demo.algorithm.lesson.basic.class1;

/**
 * @author liweizhi
 * @date 2020/4/22 17:06
 */
public class BSExist {
    public static boolean exist(int[] arr, int n) {
        if (arr == null || arr.length == 0) {
            return false;
        }
        int left = 0;
        int right = arr.length - 1;
        while (left < right) {
            int mid = left + ((right - left) >> 1);
            if (arr[mid] == n) {
                return true;
            }
            if (arr[mid] > n) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return arr[left] == n;
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7};
        System.out.println(exist(arr, 1));
    }
}
